﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectronicsMarket_Desktop.BL;
namespace ElectronicsMarket_Desktop
{
    public partial class frmPhieuNhap : Form
    {
        public frmPhieuNhap()
        {
            InitializeComponent();
        }
        ReceptDetailBL receiptdetailbl = new ReceptDetailBL();
        ReceiptBL receiptbl = new ReceiptBL();
        ProductBL productbl = new ProductBL();
        private void btnlapphieu_Click(object sender, EventArgs e)
        {
            grdchitietphieunhap.Rows.Clear();
            txtidphieunhap.Text = Convert.ToString(receiptbl.getLastPreceiptId() + 1);
            receiptbl.Receipt.RECEIPT_ID = Convert.ToInt32(txtidphieunhap.Text);
            receiptbl.Receipt.RECEIPT_DATE = DateTime.Now;
            receiptbl.Receipt.RECEIPT_TYPE = false;
            receiptbl.Receipt.RECEIPT_STAFF_ID = StaticParameter.Idnhanvien;
            receiptbl.InsertReceipt();

            
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            productbl = new ProductBL();
            receiptdetailbl = new ReceptDetailBL();
            //
            receiptdetailbl.Receipdetail.RECEIPT_ID = Convert.ToInt32( txtidphieunhap.Text);
            receiptdetailbl.Receipdetail.RECEIPT_PRODUCT_ID =Convert.ToInt32(cbosanpham.SelectedValue);
            receiptdetailbl.Receipdetail.AMOUNT = Convert.ToInt32(txtsoluong.Text);
            //
            productbl.Product.PRODUCT_ID = Convert.ToInt32(cbosanpham.SelectedValue);
            productbl.getProdcutByID();
            decimal thanhtien =Convert.ToDecimal( receiptdetailbl.Receipdetail.AMOUNT * productbl.Product.PRODUCT_PRICE);
            //
            txtthanhtien.Text = thanhtien.ToString();
            //
            receiptdetailbl.Receipdetail.UNIT_TOTAL = thanhtien;
            //
            grdchitietphieunhap.Rows.Add(receiptdetailbl.Receipdetail.RECEIPT_ID, receiptdetailbl.Receipdetail.RECEIPT_PRODUCT_ID, receiptdetailbl.Receipdetail.AMOUNT, receiptdetailbl.Receipdetail.UNIT_TOTAL);

            //
            

            
            

        }

        private void frmPhieuNhap_Load(object sender, EventArgs e)
        {
            cbosanpham.DataSource = productbl.getAllProduct();
            cbosanpham.DisplayMember = "PRODUCT_NAME";
            cbosanpham.ValueMember = "PRODUCT_ID";
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            decimal tongtien=0;
            try
            {

                for (int i = 0; i < grdchitietphieunhap.RowCount - 1; i++)
                {
                    receiptdetailbl = new ReceptDetailBL();
                    receiptdetailbl.Receipdetail.RECEIPT_ID = Convert.ToInt32(grdchitietphieunhap.Rows[i].Cells[0].Value);
                    receiptdetailbl.Receipdetail.RECEIPT_PRODUCT_ID = Convert.ToInt32(grdchitietphieunhap.Rows[i].Cells[1].Value);
                    receiptdetailbl.Receipdetail.AMOUNT = Convert.ToInt32(grdchitietphieunhap.Rows[i].Cells[2].Value);
                    receiptdetailbl.Receipdetail.UNIT_TOTAL = Convert.ToDecimal(grdchitietphieunhap.Rows[i].Cells[3].Value);
                    //
                    tongtien=Convert.ToDecimal( receiptdetailbl.Receipdetail.UNIT_TOTAL)+tongtien;
                    receiptdetailbl.InsertReceiptDeatial();
                    //

                }
                receiptbl.Receipt.RECEIPT_TOTAL = tongtien;
                receiptbl.UpdateReceipt();
                MessageBox.Show("Lưu thành công");
                
            }
            catch
            {
               
            }
        }
    }
}
